package cn.edu.zucc.JavaChat.ChatServer.Component;

import java.util.List;

import cn.edu.zucc.JavaChat.ChatServer.ClientHandler;
import cn.edu.zucc.JavaChat.ChatServer.Sender;
import cn.edu.zucc.JavaChat.ChatServer.DB.DAO.DAOFactory;
import cn.edu.zucc.JavaChat.ChatServer.DB.DAO.UserDAO;
import cn.edu.zucc.JavaChat.ChatServer.DB.Entity.User;
import cn.edu.zucc.JavaChat.Response.OnlineResponse;

public class InformIsOnline {
	
	public boolean online(String username){
		UserDAO userDAO = DAOFactory.getUserDAO();
		List<User> userList = userDAO.findAllFriend(username);
		for(User user : userList){
			ClientHandler ch = UserCenter.getInstance()
					.getClientHandler(user.getUsername());
			if(ch != null){
				OnlineResponse response = new OnlineResponse();
				response.setToUsername(user.getUsername());
				response.setOnlineUsername(username);
				response.setOnline(true);
				Sender.sendResponse(response, ch);
			}
		}
		return true;
	}
	
	public boolean offline(String username){
		UserDAO userDAO = DAOFactory.getUserDAO();
		List<User> userList = userDAO.findAllFriend(username);
		for(User user : userList){
			ClientHandler ch = UserCenter.getInstance()
					.getClientHandler(user.getUsername());
			if(ch != null){
				OnlineResponse response = new OnlineResponse();
				response.setToUsername(user.getUsername());
				response.setOnlineUsername(username);
				response.setOnline(false);
				Sender.sendResponse(response, ch);
			}
		}
		return true;
	}
}
